#include "physics.h"
#include "collider-base.h"
#include "../log.h"
#include "physics-enum.h"
#include "collider-box.h"

std::unordered_map<std::string, ColliderBase *> Physics::colliders;

Physics::Physics()
{
}

void Physics::update(float dt)
{
    // Log::print("刷新物理...."+std::to_string(Physics::colliders.size()));
    this->_updateCollider(dt);
    this->_testCollider(dt);
}
/**
 * 更新碰撞器
 */
void Physics::_updateCollider(float dt)
{
    for (auto &pair : Physics::colliders)
    {
        pair.second->update(dt);
    }
}
/**
 * 碰撞检测
 */
void Physics::_testCollider(float dt)
{
    for (auto &pair1 : Physics::colliders)
    {
        ColliderBase *collider1 = pair1.second; // 获取值（ColliderBase）
        if (!collider1->isOnCollider())
        {
            continue;
            ;
        }
        for (auto &pair2 : Physics::colliders)
        {

            ColliderBase *collider2 = pair2.second; // 获取值（ColliderBase）

            if (collider1 == collider2)
            {
                continue;
            }

            float dis = -1;
            if (collider2->getGeometry() == PhysicsColliderGeometry::Box)
            {
                dis = static_cast<ColliderBox *>(collider2)->colliderTest(collider1);
            }

            if (dis > 0)
            {
                collider1->emitColliderCall(collider2);
            }
        }
    }
}

void Physics::clean()
{
}

Physics::~Physics()
{
}